﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace AirplaneReservation
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                for (int i = 18; i < 90; i++)
                {
                    drlAge.Items.Add(i.ToString());
                }
            }
        }

        protected void btnLogIn_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid == true)
                return;
            else
            {
                SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
                ccon.Open();

                SqlCommand cmd = new SqlCommand("InsertProfile", ccon);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@userN", SqlDbType.VarChar).Value = txtUser.Text;
                cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = txtPassword.Text;
                cmd.Parameters.Add("@credit", SqlDbType.VarChar).Value = drlCredit.Text;

                cmd.Parameters.Add("@firstN", SqlDbType.NVarChar).Value = txtFirstN.Text;
                cmd.Parameters.Add("@lastN", SqlDbType.NVarChar).Value = txtLastN.Text;
                cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = txtAddress.Text;
                cmd.Parameters.Add("@phone", SqlDbType.VarChar).Value = txtPhoneNo.Text;
                cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
                cmd.Parameters.Add("@age", SqlDbType.Int).Value = drlAge.Text;
                if (drlSex.Text == "Male")
                    cmd.Parameters.Add("@sex", SqlDbType.Bit).Value = 1;
                else
                    cmd.Parameters.Add("@sex", SqlDbType.Bit).Value = 0;
                cmd.Parameters.Add("@passport", SqlDbType.VarChar).Value = txtPassport.Text;
                if (drlSex.Text == "adult passenger")
                    cmd.Parameters.Add("@type", SqlDbType.Bit).Value = 1;
                else if (drlSex.Text == "children")
                    cmd.Parameters.Add("@type", SqlDbType.Bit).Value = 2;
                else cmd.Parameters.Add("@type", SqlDbType.Bit).Value = 3;

                cmd.ExecuteNonQuery();
                cmd.Dispose();
                ccon.Close();

                Panel1.Visible = false;
                Panel2.Visible = true;
            }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
            ccon.Open();

            string sql = "select * from Member where UserName='" + txtUser.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, ccon);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
                args.IsValid = false;
            else args.IsValid = true;

            ccon.Close();
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
            ccon.Open();

            string sql = "select * from Customer where Passport='" + txtPassport.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, ccon);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
                args.IsValid = false;
            else args.IsValid = true;

            ccon.Close();
        }
    }
}